using QFramework.Framework.Util;
using UnityEditor;
using UnityEngine;

namespace QFramework.Example.Util._10.SimpleRCExample
{
    public class SimpleRcExample : MonoBehaviour
    {
        class Light
        {
            public void Open()
            {
                Debug.Log("灯打开了");
            }
        
            public void Close()
            {
                Debug.Log("灯关闭了");
            }
        }

        class Room : SimpleRc
        {
            private Light _light = new Light();
            
            public void EnterPeople()
            {
                if (RefCount == 0)
                {
                    _light.Open();
                }
                
                Retain();
                Debug.LogFormat("一个人进入房间，当前房间有{0}人", RefCount);
            }

            public void LeavePeople()
            {
                Release();
                Debug.LogFormat("一个人离开房间，当前房间有{0}人", RefCount);
            }

            protected override void OnZeroRef()
            {
                _light.Close();
            }
        }
    
        // Start is called before the first frame update
        void Start()
        {
        
        }

#if UNITY_EDITOR
        [MenuItem("QFramework/Example/Util/10.SimpleRCExample", false, 10)]
#endif
        private static void MenuClicked()
        {
            var room = new Room();
        
            room.EnterPeople();
            room.EnterPeople();
            room.EnterPeople();

            room.LeavePeople();
            room.LeavePeople();
            room.LeavePeople();
        
            room.EnterPeople();
        }
    }
}
